package my.jwds.p66;

import java.util.Arrays;

public class PlusOne {
    public static void main(String[] args) {
        PlusOne o = new PlusOne();
        int [] nums = {9,9,9,9};

        System.out.println(Arrays.toString(o.plusOne(nums)));
    }

    public int[] plusOne(int[] digits) {
        return plusOne(digits.length - 1,digits);
    }


    private int[] plusOne(int idx,int[] digits){
        if (digits[idx] == 9){
            digits[idx] = 0;
            if (idx != 0){
                return plusOne(--idx,digits);
            }else{
                int[] newArray = new int[digits.length+1];
                System.arraycopy(digits, 0, newArray, 1, digits.length);
                newArray[0] = 1;
                return newArray;
            }
        }else{
            digits[idx]++;
        }
        return digits;
    }
}
